package com.suoluo.zenadmin.controllers.portal.hotel;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import net.paoding.rose.web.Invocation;
import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;

import com.suoluo.zenadmin.common.GeometryUtil;
import com.suoluo.zenadmin.common.StringUtil;
import com.suoluo.zenadmin.common.SystemType;
import com.suoluo.zenadmin.common.UBB;
import com.suoluo.zenadmin.common.config.SystemConfigHolder;
import com.suoluo.zenadmin.service.TaskExecutor;
import com.suoluo.zenadmin.service.bigimage.BigImageService;
import com.suoluo.zenadmin.service.crawler.bean.HotelReviewBean;
import com.suoluo.zenadmin.service.dest.DestService;
import com.suoluo.zenadmin.service.hotel.HotelService;
import com.suoluo.zenadmin.vo.BaseInfoVO;
import com.suoluo.zenadmin.vo.bigimage.BigImageVO;
import com.suoluo.zenadmin.vo.country.ContinentVO;
import com.suoluo.zenadmin.vo.dest.DestFeatureCategoryVO;
import com.suoluo.zenadmin.vo.dest.DestFeatureItem2VO;
import com.suoluo.zenadmin.vo.dest.DestPathVO;
import com.suoluo.zenadmin.vo.dest.DestVO;
import com.suoluo.zenadmin.vo.hotel.AroundCategoryVO;
import com.suoluo.zenadmin.vo.hotel.AroundNodeVO;
import com.suoluo.zenadmin.vo.hotel.Hotel2VO;
import com.suoluo.zenadmin.vo.hotel.HotelAroundVO;
import com.suoluo.zenadmin.vo.hotel.HotelHomeVO;
import com.suoluo.zenadmin.vo.hotel.HotelResource2VO;
import com.suoluo.zenadmin.vo.hotel.HotelReviewRecordVO;
import com.suoluo.zenadmin.vo.hotel.HotelReviewVO;

@Path("")
public class HotelController {
	
	@Resource(name="destService")
	private DestService destService;
	
	@Resource(name="bigImageService")
	private BigImageService bigImageService;
	
	@Resource(name="hotelService")
	private HotelService hotelService;
	
	@Resource(name="taskExecutor")
	private TaskExecutor taskExecutor;
	
	
	/**
	 * 酒店的首页
	 * @param inv 上下文执行环境
	 * @return String 返回的页面
	 */
	@Get("")
	public String home(Invocation inv){
//		mv.setViewName("views/hotel/hotelOverview");
		List<ContinentVO> coninentTree = destService.findHotelNavigateTree();
		inv.addModel("coninentTree", coninentTree);
		// 需要查询出酒店精选的大图展示
		List<BigImageVO> bigImages = bigImageService
				.findBigImageByType(BigImageVO.IMAGE_TYPE_HOTEL);
		inv.addModel("bigImages", bigImages);
		return "hotelOverview";
		
	}
	
	/**
	 * 酒店最新
	 * @param inv
	 * @return
	 */
	@Get("new")
	public String showNew(Invocation inv){
		// 需要查询出酒店精选的大图展示
		List<BigImageVO> bigImages = bigImageService
				.findBigImageByType(BigImageVO.IMAGE_TYPE_HOTEL);
		inv.addModel("bigImages", bigImages);
		
		List<Hotel2VO> hotels = hotelService.findNewHotels(1);
		inv.addModel("hotels",hotels);
		return "hotelNew";
	}
	
	/**
	 * 分页加载
	 */
	@Get("page/{type:[1|2]+}/{page:[0-9]+}")
	public String showHotelPage(Invocation inv,@Param("page") int page,@Param("type") int type){
		if (type == 1){
			List<Hotel2VO> hotels = hotelService.findNewHotels(page);
			inv.addModel("hotels",hotels);
		} else {
			List<Hotel2VO> hotels = hotelService.findHotHotels(page);
			inv.addModel("hotels",hotels);
		}
		return "hotelpage";
	}
	
	/**
	 * 最热酒店
	 * @param inv
	 * @return
	 */
	@Get("hot")
	public String showHot(Invocation inv){
		// 需要查询出酒店精选的大图展示
		List<BigImageVO> bigImages = bigImageService
				.findBigImageByType(BigImageVO.IMAGE_TYPE_HOTEL);
		inv.addModel("bigImages", bigImages);
		
		List<Hotel2VO> hotels = hotelService.findHotHotels(1);
		inv.addModel("hotels",hotels);
		return "hotelHot";
	}
	
	/**
	 * 全球分布，总览
	 * @param inv
	 * @return
	 */
	@Get("overview")
	public String showOverview(Invocation inv){
		List<ContinentVO> coninentTree = destService.findHotelNavigateTree();
		inv.addModel("coninentTree", coninentTree);
		// 需要查询出酒店精选的大图展示
		List<BigImageVO> bigImages = bigImageService
				.findBigImageByType(BigImageVO.IMAGE_TYPE_HOTEL);
		inv.addModel("bigImages", bigImages);
		return "hotelOverview";
	}
	
	
	/**
	 * 列出目的地的所有酒店信息
	 * @param inv
	 * @param destId
	 * @return
	 */
	@Get("destination/{destId:[0-9]+}")
	public String listDestHotels(Invocation inv,@Param("destId")int destId){

		// 1.查询导航树
		List<ContinentVO> coninentTree = destService.findHotelNavigateTree();
		inv.addModel("coninentTree", coninentTree);
		// 2.查询目的地信息
		DestVO dest = destService.findSimplestDestById(destId);
		inv.addModel("dest", dest);

		// 3.查询目的的导航路径
		DestPathVO destPath = destService.findDestPath(destId);
		inv.addModel("destPath", destPath);

		// 4.查询目的地下的酒店信息
		List<Hotel2VO> hotels = hotelService.findHotelsWithoutContentByDestId(destId);
		inv.addModel("hotels", hotels);
		return "listDestHotels";
	}
	
	/**
	 * 单独酒店的主页
	 * 跳转到酒店的详情界面，需要查询酒店基础信息，酒店的概述资源，和酒店提供的服务
	 * @return
	 */
	@Get("{hotelId:[0-9]+}")
	public String showHotelHome(Invocation inv,@Param("hotelId") int hotelId){
		HotelHomeVO hotelHome = hotelService.findHotelHomeData(hotelId);
		inv.addModel("hotelHomeVO", hotelHome);
		
		//判断酒店是完整版还是简化版
		int viewStyle = hotelHome.getHotel().getViewStyle();
		if(viewStyle == 1)
		{
			//1.增加酒店的浏览量
			taskExecutor.submitViewHotel(hotelId);
			// 2.查询目的地
//			TravelDestinationPOJO dest = destService.findTravelDestinationById();
			DestVO dest = destService.findSimplestDestById(hotelHome.getHotel().getDestId());
			inv.addModel("dest", dest);
			// 按类别查询酒店资源:房型
			List<HotelResource2VO> roomList = new ArrayList<HotelResource2VO>();
			inv.addModel("roomList", roomList);
			// 按类别查询酒店资源:餐厅
			List<HotelResource2VO> restaurantList = new ArrayList<HotelResource2VO>();
			inv.addModel("restaurantList", restaurantList);
			// 按类别查询酒店资源:特色
			List<HotelResource2VO> specialList = new ArrayList<HotelResource2VO>();
			inv.addModel("specialList", specialList);

//			int descLength = SystemConfigHolder.getImageBlockContentLimit();
			List<HotelResource2VO> resList = hotelService.findHotelResourcesWithoutContentByHotelId(hotelId);
			for (HotelResource2VO hotelResource : resList) {
				int type = hotelResource.getResourceType();
				if (roomList.size() == 4 && restaurantList.size() == 4 && specialList.size() == 4){
					break;
				}
				if (type == SystemType.HOTEL_RESOURCE_ROOM && roomList.size() < 4) {
					roomList.add(hotelResource);
				} else if (type == SystemType.HOTEL_RESOURCE_RESTAURANT
						&& restaurantList.size() < 4) {
					restaurantList.add(hotelResource);
				} else if (type == SystemType.HOTEL_RESOURCE_SPECIAL && specialList.size() < 4) {
					specialList.add(hotelResource);
				}
			}
			return "hotelSummary";
		}
		else
		{
			//查询图文信息
			List<BaseInfoVO> list = hotelService.findHotelInfoAndImages(hotelId);
			for (BaseInfoVO base : list) {
				base.setContent(UBB.decode(base.getContent()));
				base.setZhname(StringUtil.buildCompleteName(base.getZhname(),base.getEnname()));
			}
			inv.addModel("infoImage", list);
			
			DestVO dest = destService.findSimplestDestById(hotelHome.getHotel().getDestId());
			inv.addModel("dest", dest);
			//查询周边信息
			List<DestFeatureItem2VO> items = destService.find4FeatureItems(hotelHome.getHotel().getDestId());
			List<AroundNodeVO> nodes = new ArrayList<AroundNodeVO>();
			
			for (DestFeatureItem2VO item : items){
				AroundNodeVO node = new AroundNodeVO();
				node.setNodeId(item.getFeatureItemId());
				node.setNodeName(item.getName());
				node.setRelId(item.getRelId());
				node.setImage(item.getSource() + item.getSmall());
				node.setDestId(item.getDestId());
				node.setCategoryId(item.getFeatureCategoryId());
				node.setSummary(item.getSummary());
				nodes.add(node);
				if(nodes.size() == 4) {
					break;
				}
			}
			Collections.sort(nodes);
			inv.addModel("around",nodes);
			
			return "hotelSimple";
		}
		
	
	}
	
	/**
	 * 酒店房型，餐厅，特色页面
	 * @return
	 */
	@Get("{hotelId:[0-9]+}/{type:[2|3|4]}")
	public String showHotelResources(Invocation inv,@Param("hotelId") int hotelId,@Param("type") int resourceType,
			@Param("rid") int resourceId) {

		List<HotelResource2VO> resources = hotelService.findHotelResourcesWithoutContentByHotelId(hotelId, resourceType);
		inv.addModel("navigateResource", resources);

		Hotel2VO hotel = hotelService.findSimplestHotelById(hotelId);
		inv.addModel("hotel", hotel);

		// 2.查询目的地信息
		DestVO dest = destService.findSimplestDestById(hotel.getDestId());
		inv.addModel("dest", dest);

		// 资源类型
		inv.addModel("resourceType", resourceType);

		// 酒店资源
		HotelResource2VO curResource = null;
		if (resourceId == 0 && !resources.isEmpty()){
			curResource = resources.get(0);
		} else {
			for (HotelResource2VO r : resources){
				if (r.getResourceId() == resourceId){
					curResource = r;
					break;
				}
			}
		}
		if (curResource != null){
			curResource = hotelService.findHotelResourceAndItems(curResource);
			inv.addModel("resource", curResource);
		}
		// 跳转页面
		return "hotelResourceNavigate";
	}
	
	@Get("{hotelId:[0-9]+}/around")
	public String showHotelAround(Invocation inv, @Param("hotelId") int hotelId,
			@Param("cid") int categoryId){
		Hotel2VO hotel = hotelService.findSimplestHotelById(hotelId);
		inv.addModel("hotel", hotel);
		
//		 2.查询目的地信息
		DestVO dest = destService.findSimplestDestById(hotel.getDestId());
		inv.addModel("dest", dest);

		List<DestFeatureItem2VO> items = destService.findFeatureItemsWithoutContent(hotel.getDestId(), categoryId);
		List<DestFeatureCategoryVO> categories = destService.findDestFeatureCatetories(hotel.getDestId());
		
		HotelAroundVO around = new HotelAroundVO();
		
		List<AroundNodeVO> nodes = new ArrayList<AroundNodeVO>();
		int experienceId = SystemConfigHolder.getDestFeatureCategorySpecial();
		for (DestFeatureItem2VO item : items){
			if (item.getLat() == null || item.getLng() == null){
				continue;
			}
			
			//过滤掉酒店
			if (item.getRelId() == hotel.getHotelId()){
				continue;
			}
			
			//过滤掉特别体验的数据
			if (item.getFeatureCategoryId() == experienceId){
				continue;
			}
			AroundNodeVO node = new AroundNodeVO();
			node.setNodeId(item.getFeatureItemId());
			node.setNodeName(item.getName());
			node.setRelId(item.getRelId());
			node.setImage(item.getSource() + item.getSmall());
//			node.setOrigImg((String) row.get("t_feature_item_orig_img"));
//			node.setMiniImg((String) row.get("t_feature_item_mini_img"));
			node.setLat(item.getLat());
			node.setLng(item.getLng());
			node.setDestId(item.getDestId());
			node.setCategoryId(item.getFeatureCategoryId());
			
			double distance = GeometryUtil.calcDistance(hotel,node);
//			double distance = HotelAroundVO.calcDistance(hotel.getLat().doubleValue(),
//					hotel.getLng().doubleValue(), node.getLat()
//							.doubleValue(), node.getLng().doubleValue());
			node.setDistance(distance);
			
			nodes.add(node);
		}
		Collections.sort(nodes);
		
		around.setNodes(nodes);
		
		List<AroundCategoryVO> clist = new ArrayList<AroundCategoryVO>();
		for (DestFeatureCategoryVO category : categories){
			//过滤掉特别体验
			if (category.getFeatureCategoryId() == experienceId){
				continue;
			}
			AroundCategoryVO c = new AroundCategoryVO();
			c.setCategoryId(category.getFeatureCategoryId());
			c.setCategoryName(category.getFeatureCategoryName());
			c.setCategoryOrder(category.getFeatureCategoryOrder());
			c.setCategoryColor(category.getFeatureCategoryColor());
			
			if (c.getCategoryId() == categoryId){
				around.setCurCategory(c);
			}
			clist.add(c);
		}
		Collections.sort(clist);
		
		around.setCategoryList(clist);
		inv.addModel("around",around);
		inv.addModel("hotelCategoryId", SystemConfigHolder.getDestFeatureCategoryForHotel());
		return "hotelAround";
	}
	
	
	
	/**
	 * 查看酒店周边的数据
	 * 
	 * @param request
	 * @param response
	 */
	@Get("{hotelId:[0-9]+}/around-overview")
	public String showHotelAroundOverview(Invocation inv, @Param("hotelId") int hotelId) {
		// Integer destId = Integer.valueOf(request.gHotel2VO hotel = hotelService.findSimplestHotelById(hotelId);
		Hotel2VO hotel = hotelService.findSimplestHotelById(hotelId);
		inv.addModel("hotel", hotel);

		// 2.查询目的地信息
//		TravelDestinationPOJO dest = destService.findTravelDestinationById(hotel.getDestId());
		DestVO dest = destService.findSimplestDestById(hotel.getDestId());
		inv.addModel("dest", dest);
		
		//查询所有
		List<DestFeatureItem2VO> items = destService.findFeatureItemsWithoutContent(hotel.getDestId(), 0);
		List<DestFeatureCategoryVO> categories = destService.findDestFeatureCatetories(hotel.getDestId());
		
		HotelAroundVO around = new HotelAroundVO();
		Map<Integer,AroundCategoryVO> categoryMap = new HashMap<Integer, AroundCategoryVO>();
		List<AroundCategoryVO> clist = new ArrayList<AroundCategoryVO>();
		int experienceId = SystemConfigHolder.getDestFeatureCategorySpecial();
		for (DestFeatureCategoryVO category : categories){
			//过滤掉特别体验
			if (category.getFeatureCategoryId() == experienceId){
				continue;
			}
			AroundCategoryVO c = new AroundCategoryVO();
			c.setCategoryId(category.getFeatureCategoryId());
			c.setCategoryName(category.getFeatureCategoryName());
			c.setCategoryOrder(category.getFeatureCategoryOrder());
			c.setCategoryColor(category.getFeatureCategoryColor());
			clist.add(c);
			categoryMap.put(c.getCategoryId(), c);
		}
		Collections.sort(clist);
		around.setCategoryList(clist);
		
		
		for (DestFeatureItem2VO item : items){
			//过滤掉酒店
			if (item.getRelId() == hotel.getHotelId()){
				continue;
			}
			//过滤掉特别体验的数据
			if (item.getFeatureCategoryId() == experienceId){
				continue;
			}
			
			if (item.getLat() == null || item.getLng() == null){
				continue;
			}
			AroundNodeVO node = new AroundNodeVO();
			node.setNodeId(item.getFeatureItemId());
			node.setNodeName(item.getName());
			node.setRelId(item.getRelId());
			node.setImage(item.getSource() + item.getSmall());
//			node.setOrigImg((String) row.get("t_feature_item_orig_img"));
//			node.setMiniImg((String) row.get("t_feature_item_mini_img"));
			node.setLat(item.getLat());
			node.setLng(item.getLng());
			node.setDestId(item.getDestId());
			node.setCategoryId(item.getFeatureCategoryId());
			double distance = GeometryUtil.calcDistance(hotel,node);
//			double distance = HotelAroundVO.calcDistance(hotel.getLat().doubleValue(),
//					hotel.getLng().doubleValue(), node.getLat()
//							.doubleValue(), node.getLng().doubleValue());
			node.setDistance(distance);
			
			AroundCategoryVO category = categoryMap.get(node.getCategoryId());
			if (category == null){
				continue;
			}
			category.addAroundNodeVO(node);
//			nodes.add(node);
		}
		
		for (AroundCategoryVO category : clist){
			Collections.sort(category.getNodeList());
		}
//
//		// 酒店周边信息
//		boolean allCategory = true;
////		int categoryId = SystemConfigHolder.getDestFeatureCategorySpecial();
//		if (categoryId != 0) {
//			allCategory = false;
//		}
		
//		HotelAroundVO vo = hotelService.findHotelAroundData(hotelId,
//				hotel.getDestId(), categoryId);
//		vo.setAllCategory(allCategory);
//		if (vo.getCurCategory() == null) {
//			vo.setCurCategory(vo.getCategoryList().get(0));
//		}
		inv.addModel("hotelAround", around);

		inv.addModel("hotelCategoryId",
				SystemConfigHolder.getDestFeatureCategoryForHotel());
		return "hotelAroundOverview";
	}
	
	/**
	 * 查看酒店的评论
	 */
	@Get("{hotelId:[0-9]+}/review")
	public String showHotelReview(Invocation inv, @Param("hotelId") int hotelId,@Param("rid") int reviewId) {
		// Integer destId = Integer.valueOf(request.getParameter("destId"));
		Hotel2VO hotel = hotelService.findSimplestHotelById(hotelId);
		inv.addModel("hotel", hotel);

		// 2.查询目的地信息
//		TravelDestinationPOJO dest = destService.findTravelDestinationById(hotel.getDestId());
		DestVO dest = destService.findSimplestDestById(hotel.getDestId());
		inv.addModel("dest", dest);

		// 3.查询评论信息
		List<HotelReviewVO> hotelReviews = hotelService.findListHotelReview(hotelId);
		HotelReviewVO currentHotelReview = null;
		for (HotelReviewVO review : hotelReviews) {
			int reviewType = review.getReviewType();
			// 设置四个网站的评论信息
			if (review.getReviewType() == HotelReviewBean.HotelReviewType.Daodao
					.getValue()) {
				inv.addModel("daodaoReview", review);
			} else if (reviewType == HotelReviewBean.HotelReviewType.Tripadvisor
					.getValue()) {
				inv.addModel("tripadvisorReview", review);
			} else if (reviewType == HotelReviewBean.HotelReviewType.Hotels
					.getValue()) {
				inv.addModel("hotelsReview", review);
			} else if (reviewType == HotelReviewBean.HotelReviewType.Booking
					.getValue()) {
				inv.addModel("bookingReview", review);
			}

			// 设置当前选择的评论信息
			if (reviewId != 0) {
				if (reviewId== review.getReviewId()) {
					currentHotelReview = review;
				}
			}
		}

		// 如果没有选择酒店的品论，则默认第一个
		if (currentHotelReview == null && !hotelReviews.isEmpty()) {
			currentHotelReview = hotelReviews.get(0);
		}

		inv.addModel("currentHotelReview", currentHotelReview);
		if (currentHotelReview != null) {
			// 4.查询当前选中的评论的记录信息
			List<HotelReviewRecordVO> reviewRecords = hotelService.findListHotelReviewRecord(currentHotelReview.getReviewId());
			inv.addModel("reviewRecords", reviewRecords);

			// 设置评论类型
			int curReviewType = currentHotelReview.getReviewType();
			if (curReviewType == HotelReviewBean.HotelReviewType.Daodao
					.getValue()) {
				inv.addModel("curReviewType", "daodao");
			} else if (curReviewType == HotelReviewBean.HotelReviewType.Tripadvisor
					.getValue()) {
				inv.addModel("curReviewType", "trip");
			} else if (curReviewType == HotelReviewBean.HotelReviewType.Hotels
					.getValue()) {
				inv.addModel("curReviewType", "hotel");
			} else if (curReviewType == HotelReviewBean.HotelReviewType.Booking
					.getValue()) {
				inv.addModel("curReviewType", "booking");
			}
			// 设置箭头的位置,1:daodao,2:tripadvisor,3:hotels,4:booking
			int position = 1;
			for (HotelReviewVO review : hotelReviews) {
				if (currentHotelReview.getReviewType() == review
						.getReviewType()) {
					if (position == 1) {
						inv.addModel("arrowPosition", "daodaor2");
					} else if (position == 2) {
						inv.addModel("arrowPosition", "tripr2");
					} else if (position == 3) {
						inv.addModel("arrowPosition", "hotelr2");
					} else if (position == 4) {
						inv.addModel("arrowPosition", "bookingr2");
					}
					break;
				}
				position++;
			}
		}
		return "hotelReview";
	}
	
	
}
